package com.urbo.android.shortcutter;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.Toast;

public class ContentUtils {

	/**
	 * This function retrieve phone number from Contact returned as uri from
	 * Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
	 * Contacts.CONTEN_URI is uri for all contacts, so we need to check if query
	 * resut has a phone number, retrieve phone numbers and select one (or
	 * first)
	 * 
	 * @param data
	 *            - Result from ACTION_PICK
	 * @return phone number
	 */
	public static final String getPhoneNumberFromContactUri( Context context, Uri data )
	{
		String phoneNumber = "";
		ContentResolver resolver = context.getContentResolver();

		Cursor c = resolver.query( data, null, null, null, null );
		c.moveToFirst();

		if ( c.getCount() > 0 ) {

			/*
			 * this block if useful just in case when ACTION_PICK is called with
			 * ContactsContract.Contacts.CONTENT_URI with
			 * ContactsContract.CommonDataKinds.Phone.CONTENT_URI there are just
			 * phone numbers, so HAS_PHONE_NUMBER is not present in result table
			 */
			int hasNumberColumn = c.getColumnIndex( ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER );

			if ( Integer.parseInt( c.getString( hasNumberColumn ) ) == 0 ) {
				Toast.makeText( context, "Selected contact has no phone number", Toast.LENGTH_SHORT ).show();
				return "";
			}

			Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
			String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
					ContactsContract.CommonDataKinds.Phone.NUMBER };

			String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";

			// String id = data.getLastPathSegment(); // another way how to get
			// ID of the contact
			String id = c.getString( c.getColumnIndex( ContactsContract.CommonDataKinds.Phone._ID ) );
			Cursor pCur = resolver.query( phoneUri, projection, selection, new String[] { id }, null );

			pCur.moveToFirst();

			int numberColumn = pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER );
			phoneNumber = pCur.getString( numberColumn );

			pCur.close();
			c.close();
		}

		return phoneNumber;
	}

	public static final String getEmailFromContactUri( Context context, Uri data )
	{
		String email = "";
		ContentResolver resolver = context.getContentResolver();

		Cursor c = resolver.query( data, null, null, null, null );
		c.moveToFirst();

		if ( c.getCount() > 0 ) {

			Uri phoneUri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
			String[] projection = new String[] { ContactsContract.CommonDataKinds.Email.CONTACT_ID, ContactsContract.CommonDataKinds.Email.DATA,
					ContactsContract.CommonDataKinds.Email.TYPE };

			String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?";

			// String id = data.getLastPathSegment(); // another way how to get ID
			String id = c.getString( c.getColumnIndex( ContactsContract.CommonDataKinds.Email._ID ) );
			Cursor pCur = resolver.query( phoneUri, projection, selection, new String[] { id }, null );

			if ( pCur.getCount() > 0 ) {
				pCur.moveToFirst();

				int numberColumn = pCur.getColumnIndex( ContactsContract.CommonDataKinds.Email.DATA);
				email = pCur.getString( numberColumn );

				pCur.close();
				c.close();
			} else {
				Toast.makeText( context, "Selected contact has no email address", Toast.LENGTH_SHORT ).show();
			}
		}

		return email;
	}

	
	/**
	 * This function returns phone number from contact returned as uri from
	 * Intent(Intent.ACTION_PICK,
	 * ContactsContract.CommonDataKinds.Phone.CONTENT_URI); Query result
	 * contains phone number.
	 * 
	 * @param data
	 *            - Result from ACTION_PICK
	 * @return phone number
	 */
	public static final String getPhoneNumberFromPhoneUri( Context context, Uri data )
	{
		String phoneNumber = "";
		ContentResolver resolver = context.getContentResolver();

		Cursor c = resolver.query( data, null, null, null, null );
		c.moveToFirst();

		int numberColumn = c.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER );
		phoneNumber = c.getString( numberColumn );

		return phoneNumber;
	}
	
	/**
	 * This function returns phone number from contact returned as uri from
	 * Intent(Intent.ACTION_PICK,
	 * ContactsContract.CommonDataKinds.Phone.CONTENT_URI); Query result
	 * contains phone number.
	 * 
	 * @param data
	 *            - Result from ACTION_PICK
	 * @return phone number
	 */
	public static final String getEmailNumberFromEmailUri( Context context, Uri data )
	{
		String phoneNumber = "";
		ContentResolver resolver = context.getContentResolver();

		Cursor c = resolver.query( data, null, null, null, null );
		c.moveToFirst();

		int numberColumn = c.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER );
		phoneNumber = c.getString( numberColumn );

		return phoneNumber;
	}
}
